﻿using System;
using System.Threading.Tasks;

using Tszy.Unlimited.Data.AdoNet.Sql.Providers;

namespace Tszy.Unlimited.Data.AdoNet.Sql.Components.Db.Mysql
{
    /// <summary>
    ///
    /// </summary>
    /// <typeparam name="TSource"></typeparam>
    public class ExecuteEditor<TSource> : IExecuteEditor<TSource> where TSource : new()
    {
        private readonly IDataProvider _dataProvider;

        /// <summary>
        /// 是否显示 SQL。
        /// </summary>
        private bool _showSql = false;

        /// <summary>
        /// 输出 SQL 方法。
        /// </summary>
        private Action<string> _printSql = null;

        internal ExecuteEditor(IEditor<TSource> editor, IDataProvider dataProvider, bool showSql = false, Action<string> printSql = null)
        {
            Editor = editor ?? throw new ArgumentNullException(nameof(editor));

            _dataProvider = dataProvider;

            _showSql = showSql;
            _printSql = printSql;
        }

        /// <summary>
        ///
        /// </summary>
        public IEditor<TSource> Editor { get; set; }

        #region 执行

        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public bool Execute()
        {
            var sql = SqlBuilder.BuildUpdate("", Editor.Context, _showSql, _printSql);

            return _dataProvider.Execute(sql, Editor.Context.ParameterValues) > 0;

            //var properties = Editor.Context.SetProperties.Values;
            //var where = Editor.Context.Where ?? string.Empty;

            //var updateBuilder = new StringBuilder();

            //updateBuilder.Append($"UPDATE {GetTableName()} SET ");

            //if (properties.Any())
            //{
            //    var filedBuilder = new StringBuilder();

            //    foreach (var property in properties)
            //    {
            //        if (filedBuilder.Length > 0)
            //            filedBuilder.Append(", ");

            //        filedBuilder.Append(property);
            //    }

            //    updateBuilder = updateBuilder.Append(filedBuilder);
            //}

            //if (!string.IsNullOrWhiteSpace(where))
            //{
            //    updateBuilder.Append($" WHERE {where}");
            //}

            //var sql = updateBuilder.ToString();

            //DbModelDebug.Debug(sql, Editor.Context.ParameterValues);

            //return Editor._dataManager.Execute(sql, Editor.Context.ParameterValues, Editor.Context.DbTransaction);
        }

        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public async Task<bool> ExecuteAsync()
        {
            var sql = SqlBuilder.BuildUpdate("", Editor.Context, _showSql, _printSql);

            return await _dataProvider.ExecuteAsync(sql, Editor.Context.ParameterValues) > 0;

            //var properties = Editor.Context.SetProperties.Values;
            //var where = Editor.Context.Where ?? string.Empty;

            //var updateBuilder = new StringBuilder();

            //updateBuilder.Append($"UPDATE {GetTableName()} SET ");

            //if (properties.Any())
            //{
            //    var filedBuilder = new StringBuilder();

            //    foreach (var property in properties)
            //    {
            //        if (filedBuilder.Length > 0)
            //            filedBuilder.Append(", ");

            //        filedBuilder.Append(property);
            //    }

            //    updateBuilder = updateBuilder.Append(filedBuilder);
            //}

            //if (!string.IsNullOrWhiteSpace(where))
            //{
            //    updateBuilder.Append($" WHERE {where}");
            //}

            //var sql = updateBuilder.ToString();

            //DbModelDebug.Debug(sql, Editor.Context.ParameterValues);

            //return await Editor._dataManager.ExecuteAsync(sql, Editor.Context.ParameterValues, Editor.Context.DbTransaction);
        }

        #endregion 执行
    }
}